package com.person.android.domain;

import android.util.Log;

import com.person.android.dao.FileInfo;
import com.person.android.utils.FileSearchType;
import com.person.android.utils.FormatTypeUtils;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @autor zhaokang.
 * @time on 2015/12/12.
 */
public class FileSearchTask {

    private static final String TAG = "FileSearchTask";

    private String mFilePath;                               //文件路径
    private List<FileInfo> mFileInfoList;                   //文件的信息列表

    public FileSearchTask(String filePath) {
        this.mFilePath = filePath;
    }

    private final class FileSearchRunnable implements Runnable {
        @Override
        public void run() {
            prepareAdapterDataForListView(mFilePath);
        }
    }

    private void prepareAdapterDataForListView(String rootPath) {
        try {
            if (mFileInfoList != null && mFileInfoList.size() > 0) {
                mFileInfoList.clear();
                mFileInfoList = null;
            }
            File file = new File(rootPath);
            File[] files = file.listFiles();
            int length = files.length;
            mFileInfoList = new ArrayList<>();
            for (int i = 0; i < length; i++) {
                Date date = new Date(files[i].lastModified());
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                String info = format.format(date);
                FileInfo tempInfo = new FileInfo();
                String path = files[i].getPath();
                /*if (path.length() >= 25) {
                    path = "../" + files[i].getName();
                }*/
                tempInfo.filePath = path;
                tempInfo.lastModifiedInfo = info;
                if (files[i].isDirectory()) {
                    tempInfo.fileNum = FormatTypeUtils.countValidSubFilesNum(files[i].getPath());
                    tempInfo.type = FileInfo.FileType.FOLDER;
                    if (tempInfo.fileNum > 0) {
                        mFileInfoList.add(tempInfo);
                    }
                } else {
                    String formats = FormatTypeUtils.getExtensionName(files[i].getPath());
                    if (FormatTypeUtils.isAudioFormat(formats)) {
                        tempInfo.type = FileInfo.FileType.AUDIO;
                        mFileInfoList.add(tempInfo);
                    }
                }
            }
            notifyActivityUpdate(mFileInfoList, FileSearchType.TRAVERSE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            Log.i(TAG, "file search exception");
            notifyActivityUpdate(mFileInfoList, FileSearchType.TRAVERSE_EXCEPTION);
        }
    }

    private void notifyActivityUpdate(List<FileInfo> fileInfoList, int type) {
        if (mOnFileSearchListener != null) {
            mOnFileSearchListener.onNotifyActivityUpdate(fileInfoList, type);
        }
    }

    private OnNotifyActivityUpdateListener mOnFileSearchListener;

    public FileSearchTask setOnFileSearchListener(OnNotifyActivityUpdateListener listener) {
        this.mOnFileSearchListener = listener;
        return this;
    }

    public interface OnNotifyActivityUpdateListener {
        void onNotifyActivityUpdate(List<FileInfo> fileInfoList, int type);
    }

    private FileSearchRunnable mFileSearchRunnable;

    public void start() {
        mFileSearchRunnable = new FileSearchRunnable();
        new Thread(mFileSearchRunnable).start();
    }

    public void stop() {

    }
}
